package toss.model;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/**
 * Represents a single real life social event. (for example a concert)
 * 
 * @author BogDamnIt
 * 
 */
public class Event {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String _name;
	@Persistent
	private String _country;
	@Persistent
	private String _city;
	@Persistent
	private String _zipcode;
	@Persistent
	private String _address;
	@Persistent
	private GregorianCalendar _date;
	@Persistent
	private int _price;
	@Persistent
	private int _capacity;
	@Persistent
	private boolean _isSoldOut;

	/**
	 * Creates a new, blank event.
	 */
	public Event() {
	}

	/**
	 * Creates a new Event with the specified values.
	 * 
	 * @param name
	 *            String
	 * @param country
	 *            String
	 * @param city
	 *            String
	 * @param zipcode
	 *            String
	 * @param address
	 *            String
	 * @param date
	 *            Date
	 * @param price
	 *            int
	 * @param capacity
	 *            int
	 * @param isSoldOut
	 *            boolean
	 */
	public Event(String name, String country, String city, String zipcode,
			String address, GregorianCalendar date, int price, int capacity,
			boolean isSoldOut) {
		this._name = name;
		this._country = country;
		this._city = city;
		this._zipcode = zipcode;
		this._address = address;
		this._date = date;
		this._price = price;
		this._capacity = capacity;
		this._isSoldOut = isSoldOut;
	}

	/**
	 * Creates a new Event with the specified values.
	 * 
	 * @param name
	 *            String
	 * @param country
	 *            String
	 * @param city
	 *            String
	 * @param zipcode
	 *            String
	 * @param address
	 *            String
	 * @param day
	 *            int
	 * @param month
	 *            int
	 * @param year
	 *            int
	 * @param price
	 *            int
	 * @param capacity
	 *            int
	 * @param isSoldOut
	 *            boolean
	 */
	public Event(String name, String country, String city, String zipcode,
			String address, int day, int month, int year, int price,
			int capacity, boolean isSoldOut) {
		this._name = name;
		this._country = country;
		this._city = city;
		this._zipcode = zipcode;
		this._address = address;
		this._date = createDate(day, month, year);
		this._price = price;
		this._capacity = capacity;
		this._isSoldOut = isSoldOut;
	}

	public String getName() {
		return _name;
	}

	public void setName(String name) {
		this._name = name;
	}

	public String getCountry() {
		return _country;
	}

	public void setCountry(String country) {
		this._country = country;
	}

	public String getCity() {
		return _city;
	}

	public void setCity(String city) {
		this._city = city;
	}

	public String getZipcode() {
		return _zipcode;
	}

	public void setZipcode(String zipcode) {
		this._zipcode = zipcode;
	}

	public String getAddress() {
		return _address;
	}

	public void setAddress(String address) {
		this._address = address;
	}

	public GregorianCalendar getDate() {
		return _date;
	}

	public void setDate(GregorianCalendar date) {
		this._date = date;
	}

	public int getPrice() {
		return _price;
	}

	public void setPrice(int price) {
		this._price = price;
	}

	public int getCapacity() {
		return _capacity;
	}

	public void setCapacity(int capacity) {
		this._capacity = capacity;
	}

	public boolean isIsSoldOut() {
		return _isSoldOut;
	}

	public void setIsSoldOut(boolean isSoldOut) {
		this._isSoldOut = isSoldOut;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Event name: ");
		builder.append(this._name);
		builder.append("at ");

		if (isNullOrEmpty(this._city))
			builder.append(_city);
		else
			builder.append("N/A");

		return builder.toString();
	}

	public static GregorianCalendar createDate(int day, int month, int year) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		return calendar;
	}

	private boolean isNullOrEmpty(String toBeChecked) {
		if (toBeChecked == null || toBeChecked == "")
			return false;
		return true;
	}
}
